package com.example.projectmobile;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailActivity extends Activity {
	
	private PlaceDataSource datasrc;
	private ArrayList<Place> placeSQLData;
	private int rid;
	private String name;
	private String des;
	private double lat;
	private double lon;
	private String pic;
	private int id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		
		Bundle bundle = getIntent().getExtras();
		id = bundle.getInt("ID");//get id from list
		
		datasrc=new PlaceDataSource(this);
		datasrc.open();
		
		placeSQLData = datasrc.getAllPlace(id);//get data where id = select id

		for (int i = 0; i < placeSQLData.size(); i++) {
			rid=placeSQLData.get(i).getId();
			name=placeSQLData.get(i).getName();
			des=placeSQLData.get(i).getDes();
			lat=placeSQLData.get(i).getLat();
			lon=placeSQLData.get(i).getLon();
			pic=placeSQLData.get(i).getPic();
		}
		
		//show detail
		TextView textName = (TextView)findViewById(R.id.text_name);
		textName.setText("Name : "+name);
		TextView textDes = (TextView)findViewById(R.id.text_des);
		textDes.setText("Description : "+des);
		TextView textLat = (TextView)findViewById(R.id.text_lat);
		textLat.setText("Latitude : "+lat);
		TextView textLon = (TextView)findViewById(R.id.text_long);
		textLon.setText("Longitude : "+lon);
		
		File imgFile = new File(Environment.getExternalStorageDirectory(),"MyImages/"+pic);//get image
		if(imgFile.exists()){
			Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
			ImageView myImg = (ImageView) findViewById(R.id.imageView1);
			myImg.setImageBitmap(myBitmap);
		}else{
			System.out.println("No img");
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detail, menu);
		return true;
	}
	
	public void onClickMap(View view) {
		Intent intent = new Intent(DetailActivity.this, MapActivity.class);
		
		intent.putExtra("mapLat", lat);
		intent.putExtra("mapLon", lon);
		intent.putExtra("mapName", name);
		
		startActivity(intent);//start mapActivity
	}
	public void onClickDelete(View view) {
		new AlertDialog.Builder(this)//confirm delete
		.setTitle("Confirmation")
		.setMessage("Are you sure?")
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

		    public void onClick(DialogInterface dialog, int whichButton) {
		    	datasrc.deletePlace(id);//delete
				
				Intent intent = new Intent(DetailActivity.this, MainActivity.class);		
				startActivity(intent);//return to MainActivity
		    }})
		 .setNegativeButton(android.R.string.no, null).show();
		
	}

}
